package disorderly;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

public class Event implements Comparable<Event> {
  public int Hour;
  public int Month;
  public int Year;
  public int Minute;
  public int Day;
  public String Title;
  public Attendance[] Attendance;

  @Override
  public boolean equals(Object obj) {
    return EqualsBuilder.reflectionEquals(this, obj, "Attendance");
  }

  @Override
  public int hashCode() {
    return HashCodeBuilder.reflectionHashCode(this, "Attendance");
  }

  @Override
  public String toString() {
    return Title + ": " + (Day < 10 ? "0" + Day : Day) + "." + (Month < 10 ? "0" + Month : Month) + "." + Year;
  }

  public List<Attendance> getSortedAttendance() {
    if (Attendance == null) {
      return null;
    }
    List<Attendance> list = Arrays.asList(Attendance);
    Collections.sort(list);
    return list;
  }

  public boolean isReset() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, Year);
    cal.set(Calendar.MONTH, Month - 1);
    cal.set(Calendar.DAY_OF_MONTH, Day);
    cal.set(Calendar.HOUR_OF_DAY, Hour);
    cal.set(Calendar.MINUTE, Minute);
    if (cal.get(Calendar.YEAR) == 2011 && cal.get(Calendar.MONTH) == 11 && cal.get(Calendar.DAY_OF_MONTH) == 1) {
      return true;
    }
    return cal.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY;
  }

  public Calendar getCalDate() {
    Calendar calThis = Calendar.getInstance();
    calThis.set(Calendar.YEAR, Year);
    calThis.set(Calendar.MONTH, Month - 1);
    calThis.set(Calendar.DAY_OF_MONTH, Day);
    calThis.set(Calendar.HOUR_OF_DAY, Hour);
    calThis.set(Calendar.MINUTE, Minute);
    return calThis;
  }

  @Override
  public int compareTo(Event o) {
    Calendar calO = o.getCalDate();

    Calendar calThis = getCalDate();
    return calThis.getTime().compareTo(calO.getTime());
  }

  public String getDate() {
    return (Day < 10 ? "0" + Day : Day) + "." + (Month < 10 ? "0" + Month : Month);
  }
}
